﻿using AjaxControlToolkit;
using ASP;
using IPMS.AccessAuth;
using IPMS.BLL;
using IPMS.Sort;
using IPMS.Common;
using System;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using Resources;
using System.Web;
using SysOperationLayer.DBHandle;
using System.Data;
using SysComm;

public partial class SysUsersAdd : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        string strCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();

        try
        {
            if (Session["Culture"] != null)
            {
                strCulture = Session["Culture"].ToString();
            }
        }
        catch (NullReferenceException)
        {
        }

        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(strCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(strCulture);
        base.InitializeCulture();
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!SysUtil.IsAdministratorUsertype())
        {
            base.Response.Redirect("ErrorPage/NotAllowed.aspx");
        }
        else if (!Page.IsPostBack)
        {
            ((Label)this.Master.FindControl("lblMstTitle")).Text = " | " + ISitemap.Settings + " | " + ISitemap.UserSettings.ToString() + " | " + ISitemap.UserAdd;
            ddlGroup.Items.Add(new ListItem("", ""));
            GroupsDB objDB = new GroupsDB();
            DataTable dt = objDB.getUserGroupList("");
            objDB.CloseDB();

            if (dt != null)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ddlGroup.Items.Add(new ListItem(objDB.GetValue(dt.Rows[i], "GroupName"), objDB.GetValue(dt.Rows[i], "Rowno")));
                }
            }

            lstUserRole.Items.Add("Management");
            lstUserRole.Items.Add("Supervisor");
            lstUserRole.Items.Add("User");
            lstUserRole.Items.Add("Engineer");
            lstUserRole.SelectedIndex = 2;

            lstSuspend.Items.Add("Y");
            lstSuspend.Items.Add("N");
            lstSuspend.SelectedIndex = 0;
        }
    }

    protected void OnClick(object sender, EventArgs e)
    {
        if (sender == btnSave)
        {
            UsersDB objDB = new UsersDB();

            if (objDB.Insert(ddlGroup.SelectedValue.Trim(), txtLoginName.Text.Trim(), txtPasswd.Text.Trim(), txtUserName.Text.Trim(), txtEmail.Text.Trim(), txtPhoneNum.Text.Trim(), txtAddress.Text.Trim(), txtRemark.Text.Trim(), lstSuspend.SelectedValue, lstUserRole.SelectedIndex + 1, int.Parse(txtAdvanceAlertDay.Text.Trim())) == 0)
            {
                new LogDB(objDB.GetDB()).writeSucLog(SysUtil.m_functiong_user_manage, "Create User Account Success!");
                objDB.CloseDB();
                //Jscript.Alert("Create User Account Success!");
                //Response.Redirect("SysUsers.aspx");
                Jscript.AlertAndRedirect("Create User Account Success!", "SysUsers.aspx", this.Page);
            }
            else
            {
                new LogDB(objDB.GetDB()).writeFaileLog(SysUtil.m_functiong_user_manage, "Create User Account Failure!");
                objDB.CloseDB();
                Jscript.Alert("Create User Account Failure!", this.Page);
                lblError.Text = objDB.ErrorMessage;
            }
        }
        else if (sender == btnCancel)
        {
            txtAddress.Text = "";
            txtEmail.Text = "";
            txtLoginName.Text = "";
            txtPasswd.Text = "";
            txtPhoneNum.Text = "";
            txtUserName.Text = "";
            txtRemark.Text = "";
            ddlGroup.SelectedIndex = 0;
            lblError.Text = "";
        }
        else if (sender == btnBack)
        {
            Response.Redirect("SysUsers.aspx");
        }
    }
}
